const app = getApp()
Page({
  data: {
    arr:[],
    queNum:'',
    score:['white','white','white','white','grey','B598A1','B598A1'],
    qnum: 0,
    question:'',
    optionA:'',
    optionB:'',
    optionC:'',
    optionD:'',
    defen:0,
    num:180
  },
  btnA:function(e){
    //var that = this
    var temp=this.data.score;
    let num=this.data.text[this.data.qnum].qId
    //console.log(num)
      for(var i=0;i<=3;i++){
        temp[i]='white';
        if(i == 0){
          temp[i] = '#B598A1'
        }
      }
      this.setData({
        score:temp,
        arr:this.data.arr.concat({qId:num,anser:'A',torf:false})
      })
      wx.setStorageSync('answer',this.data.arr)
      if(this.data.qnum<this.data.text.length-1){
        this.qnext()
      }
      else{
        this.submit()
      }
  },
  btnB:function(e){
    var temp=this.data.score;
    let num = this.data.queNum
      for(var i=0;i<=3;i++){
        temp[i]='white';
        if(i == 1){
          temp[i] = '#B598A1'
        }
      }
      this.setData({
        score:temp,
        arr:this.data.arr.concat({qId:num,anser:'B',torf:false})
      })
      wx.setStorageSync('answer',this.data.arr)
      if(this.data.qnum<this.data.text.length-1){
        this.qnext()
      }
      else{
        this.submit()
      }
  },
  btnC:function(e){
    let num = this.data.queNum
    let arr = this.data.arr
    //console.log(arr)
      var temp=this.data.score;
      for(var i=0;i<=3;i++){
        temp[i]='white';
        if(i == 2){
          temp[i] = '#B598A1'
        }
      }
      this.setData({
        score:temp,
        arr:this.data.arr.concat({qId:num,anser:'C',torf:false})
      })
      wx.setStorageSync('answer',this.data.arr)
      if(this.data.qnum<this.data.text.length-1){
        this.qnext()
      }
      else{
        this.submit()
      }
      // console.log(this.data.arr)
  },
  btnD:function(e){
    let num = this.data.queNum
    var temp=this.data.score;
      for(var i=0;i<=3;i++){
        temp[i]='white';
        if(i == 3){
          temp[i] = '#B598A1'
        }
      }
      this.setData({
        score:temp,
        arr:this.data.arr.concat({qId:num,anser:'D',torf:false})
      })
     // console.log(this.data.arr)
      wx.setStorageSync('answer',this.data.arr)
      if(this.data.qnum<this.data.text.length-1){
        this.qnext()
      }
      else{
        this.submit()
      }
  },
  qnext: function(){
    var temp=this.data.score;
    for(var i=0;i<=3;i++){
      temp[i]='white';
    }
    temp[4]='#B598A1'
    if(this.data.qnum==8){
      temp[5]='grey'
    }
    if(this.data.qnum<9){
      this.setData({
        qnum: this.data.qnum+1,
        question: this.data.text[this.data.qnum+1].qContext,
        optionA:this.data.text[this.data.qnum+1].opA,
        optionB:this.data.text[this.data.qnum+1].opB,
        optionC:this.data.text[this.data.qnum+1].opC,
        optionD:this.data.text[this.data.qnum+1].opD,
        score:temp,
        // record:{'this.data.qnum':this.data.text[this.data.qnum].qId},
        queNum:this.data.text[this.data.qnum+1].qId
      })
      // console.log(this.data.text[this.data.qnum].qId);
    }
},
  qprev:function(){
    var temp=this.data.score;
    for(var i=0;i<=3;i++){
      temp[i]='white';
    }
    temp[5]='#B598A1'
    if(this.data.qnum==1){
      temp[4]='grey'
     }
    if(this.data.qnum>=1){
    this.setData({
      qnum: this.data.qnum-1,
      question: this.data.text[this.data.qnum-1].qContext,
      optionA:this.data.text[this.data.qnum-1].opA,
      optionB:this.data.text[this.data.qnum-1].opB,
      optionC:this.data.text[this.data.qnum-1].opC,
      optionD:this.data.text[this.data.qnum-1].opD,
      score:temp,
    })
  }
  },
  submit:function(){
    let token = wx.getStorageSync('token')
    var temp=this.data.score;
    for(var i=0;i<=3;i++){
      temp[i]='white';
    }
    this.setData({
      score:temp,
    })
//dbdbc
    for(var i=0;i<this.data.text.length;i++){
      let rightan = this.data.text[i].answer.slice(0,1)
      //console.log(rightan)
      if(this.data.arr[i].anser==rightan){
        this.setData({
        [`arr[${i}].torf`]:true,
        defen:this.data.defen+1,
        })
        // arr:this.data.arr.concat({qId:num,anser:'D',torf:false})
        console.log(this.data.arr[i].torf)
      }
      wx.setStorageSync('answer', this.data.arr)
    }
    console.log(this.data.defen)
    let brr = wx.getStorageSync('answer')
    wx.request({
      url: `${app.globalData.url}/record`,
      method:'POST',
          dataType:'text',
          header:{
            'content-type':'application/x-www-form-urlencoded'
          },
          data:{
          brr:brr,
          uId:token,
          defen:this.data.defen
          },
          success:res =>{
            console.log(res)
          }
    })

    wx.redirectTo({
      url: '/pages/play5/play5',
    })
  },
  goToPlay3:function(){
    wx.navigateBack({
     delta:1,
    })
  },
  numDown:function(){
    var that = this,
    num = that.data.num;
    setTimeout(function(){
      that.setData({
        num:num-1 
      })
      that.endNum()
    },1000)
  },
  endNum:function(){
    var that = this,
    num = that.data.num;
    if(num ==0){
      wx.navigateBack({
        delta: 1,
      })
      this.submit()
    }else{
      that.numDown()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pId=options.pId;
    wx.request({
      url: `${app.globalData.url}/qa/?pId=${pId}`,
      success: res=>{
        this.setData({
          // text: res.data.data[0].qContext,
            text: res.data.data,
           question:res.data.data[0].qContext,
           optionA:res.data.data[0].opA,
           optionB:res.data.data[0].opB,
           optionC:res.data.data[0].opC,
           optionD:res.data.data[0].opD,
        })
        console.log(res.data.data)
      }
    })
    console.log(pId)
    this.numDown()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})